package com.calendar.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;
import java.util.Collections;

@Configuration
public class CorsConfig {
    
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        
        // 允许所有源
        config.setAllowedOriginPatterns(Collections.singletonList("*"));
        // 允许所有请求头
        config.setAllowedHeaders(Arrays.asList("*"));
        // 允许所有方法
        config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
        // 允许携带凭证
        config.setAllowCredentials(true);
        // 设置预检请求的缓存时间（秒）
        config.setMaxAge(3600L);
        // 暴露响应头
        config.setExposedHeaders(Arrays.asList("Authorization", "Content-Type", "X-Requested-With"));
        
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
} 